using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;
using DTN.Core.Common;
using DTN.Core.Utilities;
namespace com.dtn.deals
{
	public class tbl_ImagesManager : Itbl_ImagesManager
	{
		private static Itbl_ImagesManager itbl_ImagesManager = null;
		private tbl_ImagesManager()
		{
		}
		public static Itbl_ImagesManager CreateInstance()
		{
			if (itbl_ImagesManager == null)
			{
				itbl_ImagesManager = new tbl_ImagesManager();
			}
			return itbl_ImagesManager;
		}
		public int Inserttbl_Images(tbl_Images tbl_images)
		{
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
																new SqlParameter("@proId", SqlDbType.Int),
																new SqlParameter("@imageTitle", SqlDbType.NVarChar),
																new SqlParameter("@listImage", SqlDbType.NVarChar),
                                                                new SqlParameter("@path", SqlDbType.VarChar),
                                                                new SqlParameter("@type", SqlDbType.Int),
																new SqlParameter("@cDate", SqlDbType.DateTime),
															};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = tbl_images.proId;
			parameters[2].Value = tbl_images.imageTitle;
			parameters[3].Value = tbl_images.listImage;
            parameters[4].Value = tbl_images.parth;
            parameters[5].Value = tbl_images.type;
			parameters[6].Value = tbl_images.cDate;
			try
			{
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Inserttbl_Image", CommandType.StoredProcedure, parameters);
				tbl_images.ID = Convert.ToInt32(parameters[0].Value);
			}
			catch (Exception ex)
			{
				tbl_images.ID = -1;
			}
			return tbl_images.ID;
		}
		public bool Updatetbl_Images(tbl_Images tbl_images)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
																new SqlParameter("@proId", SqlDbType.Int),
																new SqlParameter("@imageTitle", SqlDbType.NVarChar),
																new SqlParameter("@listImage", SqlDbType.NVarChar),
                                                                new SqlParameter("@path", SqlDbType.VarChar),
                                                                new SqlParameter("@type", SqlDbType.Int),
																new SqlParameter("@cDate", SqlDbType.DateTime),
															};
			parameters[0].Value = tbl_images.ID;
			parameters[1].Value = tbl_images.proId;
			parameters[2].Value = tbl_images.imageTitle;
			parameters[3].Value = tbl_images.listImage;
            parameters[4].Value = tbl_images.parth;
            parameters[5].Value = tbl_images.type;
            parameters[6].Value = tbl_images.cDate;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Updatetbl_Images", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
        public bool Updatetbl_ImagesByProID(int proID) 
        {
            bool bresult = false;
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@proID", SqlDbType.Int),
															};
            parameters[0].Value = proID;
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Updatetbl_ImagesByProId", CommandType.StoredProcedure, parameters);
                bresult = true;
            }
            catch (Exception ex)
            {
                bresult = false;
            }
            return bresult;
        }
		public bool Deletetbl_Images(int id)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Deletetbl_Image", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}

        public bool Deletetbl_ImagesDynamic(string Where) 
        {
            bool bresult = false;
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar),
															};
            parameters[0].Value = Where;
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Deletetbl_ImagesDynamic", CommandType.StoredProcedure, parameters);
                bresult = true;
            }
            catch (Exception ex)
            {
                bresult = false;
            }
            return bresult;
        }
		public tbl_Images Selecttbl_ImagesByID(int id)
		{
			IDataReader dr;
			tbl_Images tbl_images = new tbl_Images();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				dr = dataProvider.ExecuteReader("sp_Selecttbl_Images", CommandType.StoredProcedure, parameters);
				while(dr.Read())
				{
					tbl_images.ID = Convert.ToInt32(dr["ID"].ToString());
					tbl_images.proId = Convert.ToInt32(dr["proId"].ToString());
					tbl_images.imageTitle = (dr["imageTitle"].ToString());
					tbl_images.listImage = (dr["listImage"].ToString());
					tbl_images.cDate = Convert.ToDateTime(dr["cDate"].ToString());
				}

			}
			catch (Exception ex)
			{
				return null;
			}
			return tbl_images;
		}
		public DataTable Selecttbl_ImagessDynamic(string Where, string Order)
		{
			DataSet ds = new DataSet();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
			parameters[0].Value = Where;
			parameters[1].Value = Order;
			try
			{
                ds = dataProvider.ExecuteDataSet("sp_Selecttbl_ImagesDynamic", CommandType.StoredProcedure, parameters);
			}
			catch (Exception ex)
			{
				return null;
			}
			return ds.Tables["Table"];
		}

        //Cuong Code
        public static DataTable GetAndPagingByType(int objid,int type, int _pageSize,
                                                  int _currentPage, out int _totalRecord)
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);

            SqlCommand dbCmd = new SqlCommand("tbl_Images_GetAndPagingByType", dbConn);
            dbCmd.Parameters.AddWithValue("@PageSize", _pageSize);
            dbCmd.Parameters.AddWithValue("@PageIndex", _currentPage);
            dbCmd.Parameters.AddWithValue("@Type", type);
            dbCmd.Parameters.AddWithValue("@ObjId", objid);
            dbCmd.Parameters.Add(new SqlParameter("@Count", SqlDbType.Int));
            dbCmd.Parameters["@Count"].Direction = ParameterDirection.Output;
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

                _totalRecord = ConvertUtility.ToInt32(dbCmd.Parameters["@Count"].Value);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
	}
}
